/* 
 * A664p7_internal.h -- A664P7 over Ethernet library functions for internal use only
 *                    See implementation files for documentation
 *
 */

#ifndef __INCLUDE_A664P7_INTERNAL_H__
#define __INCLUDE_A664P7_INTERNAL_H__

#include "protocol.h"
#include "A664p7_api.h"
#include "pbuf.h"

#define MIN_A664P7_MESSAGE_SIZE 17

/* Ethernet protocol functions */
int eh_to_net(ETHERNET_HEADER *h);
int eh_from_net(ETHERNET_HEADER *h);
int initialize_ethernet_header(ETHERNET_HEADER *h);
int populate_ethernet_header(ETHERNET_HEADER *h, unsigned char *source_mac, unsigned char *dest_mac, unsigned short int type);

/* IP functions */
unsigned short int generate_ip_checksum(IP_HEADER *hdr);
int ih_to_net(IP_HEADER *h);
int ih_from_net(IP_HEADER *h);
int initialize_ip_header(IP_HEADER *h);
int populate_ip_header(IP_HEADER *h, unsigned int src_ip, unsigned int dst_ip);

/* Packet handling functions */
int convert_packet_header_byte_order(PACKET *p, int order);
int setup_packet_header_pointers(PACKET *p);
int setup_packet_pointers(PACKET *p);
int destroy_packet(PACKET *pkt);
PACKET  *create_packet(int max_data_length);
int copy_packet(PACKET *dest, PACKET *source);
int send_A664p7_packet(PACKET *p);
PACKET *read_A664p7_packet(struct pbuf *pbuf);

/* support functions */
void mem_die(void *pointer);
void show_mac_address(unsigned char addr[6]);
void show_eth_header(PACKET *p);
char *iptos(unsigned long in, char *out);
int get_int(char *prompt, int rangel, int rangeh);

/* UDP functions */
void generate_udp_checksum(PACKET *pack, int use_checksum);
void uh_to_net(UDP_HEADER *h);
void uh_from_net(UDP_HEADER *h);
void initialize_udp_header(UDP_HEADER *h);
void populate_udp_header(UDP_HEADER *h, unsigned short int s_port, unsigned short int d_port);

/* VL Handling functions*/
VL_LIST *push_vl(VL_LIST *list_top, VL *vl);
void destroy_vl(VL *vl);
int populate_vl(VL *vl, PACKET *pkt, int vl_num, int vl_mode);
VL *create_vl(int vl_num, int vl_mode, int max_message_size, 
				 unsigned char src_mac[6],
				 unsigned int src_ip, unsigned int dst_ip,
				 unsigned short int src_port, unsigned short int dst_port);
int rx_vl_packet_match(PACKET *p, VL *vl);
int count_flag_bits(unsigned int bitfield);
int distribute_packet_to_vls(PACKET *p, VL_LIST *vls, int *reject_flags);

void print_vl_flags(int vl_flags);

#endif
